package com.chenzhou.product_sys.controller;

import com.chenzhou.product_sys.common.Encrypt;
import com.chenzhou.product_sys.entity.UserEntity;
import com.chenzhou.product_sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService service;

    @RequestMapping("/login")
    public String user_Login(HttpSession session, String uname, String password){
        ///TODO 将用户名和密码拿到数据库进行比对，看输入的用户名或者密码是否正确
        //对用户输入的密码加密进行验证
        String pwd = Encrypt.md5AndSha(password);
        UserEntity user = service.queryUserByUnameAndPwd(uname,pwd);
        boolean flag = null!=user?true:false;

        ///TODO 如果用户和密码数据库存在，那么进入到系统里面
        if(flag){
            if(user.getIsadmin().equals("Y")){
                session.setAttribute("user",user);
                return "redirect:/user/login_main";
            }else{
                session.setAttribute("msg","不是管理员不能登录！！！");
                return "redirect:/index.jsp";
            }
        }else{
            session.setAttribute("msg","用户名或者密码是不正确！！！！");
            return "redirect:/index.jsp";
        }
    }

    @RequestMapping("/login_out")
    public String login_Out(HttpServletRequest request){
        return "redirect:/index.jsp";
    }

    @RequestMapping("/login_main")
    public String login_main(HttpServletRequest request){
        ///TODO  图表中的统计数据必须查询出来
        //HttpSession session = request.getSession();
        request.setAttribute("menu","Dashboard");
        return "main";
    }
}
